# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter


class CrawlspiderdemoPipeline:
    def process_item(self, item, spider):
        return item


# import json
#
#
# # 定义图书信息保存到文件的管道
# class BooksChinapiderPipeline(object):
#
#     def open_spider(self, spider):
#         if spider.name == "books":
#             self.file = open('books.json', 'a', encoding='utf-8')
#
#     def process_item(self, item, spider):
#         if spider.name == "books":
#             self.file.write(json.dumps(dict(item), ensure_ascii=False, indent=2) + ',\n')
#         return item
#
#     def close_spider(self, spider):
#         if spider.name == "books":
#             self.file.close()
#
#
from pymongo import MongoClient


# 定义图书信息保存到 MongoDB 数据库的管道
class MongodbPipeline(object):
    # 爬虫开启时执行一次
    def open_spider(self, spider):
        # spider对象.属性 == 爬虫名字
        if spider.name == "books":
            con = MongoClient()
            self.collection = con.bookchina.books

    def process_item(self, item, spider):
        if spider.name == "books":
            self.collection.insert(dict(item))
        return item
